<?php
/*
 * Takes a JSON string from an api call and the new number of servings from a user input.
 * Identically formatted JSON string with the new measurements is in variable $str at the
 * location marked "DONE CONVERTING".
 *
 * Returns rounded decimal (not exact or fraction) values.
 */
 
$rid = $_POST['rid'];
$new_yield = $_POST['new_yield'];
$obj = json_decode(get("recipe/$rid",null));

$flag = true;

foreach($obj->{"ingredients"} as $x) {
	$x->{"quantity"} = $x->{"quantity"} * $new_yield / $obj->{"yield"};
}

while($flag) {
	$str = json_encode($obj);
	scale_units($obj, $new_yield, $obj->{"yield"});
	if(strcmp($str, json_encode($obj)) == 0) $flag = false;
}

$obj->{"yield"} = $new_yield;

// DONE CONVERTING
echo $str;

function scale_units(&$obj) {
	foreach($obj->{"ingredients"} as $x) {
		if($x->{"abbrev"} == "tsp" && $x->{"quantity"} >= 3) {
			$x->{"quantity"} = (round($x->{"quantity"} * 10 / 3) / 10);
			$x->{"abbrev"} = "tbsp";
			$x->{"measurement"} = "Tablespoon";
		} else if($x->{"abbrev"} == "tbsp" && $x->{"quantity"} >= 16) {
			$x->{"quantity"} = (round($x->{"quantity"} * 125 / 2) / 1000);
			$x->{"abbrev"} = "c";
			$x->{"measurement"} = "Cup";
		} else if($x->{"abbrev"} == "fl oz" && $x->{"quantity"} >= 2) {
			$x->{"quantity"} = (round($x->{"quantity"} * 125) / 1000);
			$x->{"abbrev"} = "c";
			$x->{"measurement"} = "Cup";
		} else if($x->{"abbrev"} == "c" && $x->{"quantity"} >= 2) {
			$x->{"quantity"} = (round($x->{"quantity"} * 500) / 1000);
			$x->{"abbrev"} = "pt";
			$x->{"measurement"} = "Pint";
		} else if($x->{"abbrev"} == "pt" && $x->{"quantity"} >= 2) {
			$x->{"quantity"} = (round($x->{"quantity"} * 500) / 1000);
			$x->{"abbrev"} = "qt";
			$x->{"measurement"} = "Quart";
		} else if($x->{"abbrev"} == "qt" && $x->{"quantity"} >= 4) {
			$x->{"quantity"} = (round($x->{"quantity"} * 250) / 1000);
			$x->{"abbrev"} = "gal";
			$x->{"measurement"} = "Gallon";
		} else if($x->{"abbrev"} == "gal" && $x->{"quantity"} <= 0.25) {
			$x->{"quantity"} *= 4;
			$x->{"abbrev"} = "qt";
			$x->{"measurement"} = "Quart";
		} else if($x->{"abbrev"} == "qt" && $x->{"quantity"} <= 0.5) {
			$x->{"quantity"} *= 2;
			$x->{"abbrev"} = "pt";
			$x->{"measurement"} = "Pint";
		} else if($x->{"abbrev"} == "pt" && $x->{"quantity"} <= 0.5) {
			$x->{"quantity"} *= 2;
			$x->{"abbrev"} = "c";
			$x->{"measurement"} = "Cup";
		} else if($x->{"abbrev"} == "c" && $x->{"quantity"} <= 0.125) {
			$x->{"quantity"} *= 16;
			$x->{"abbrev"} = "tbsp";
			$x->{"measurement"} = "Tablespoon";
		} else if($x->{"abbrev"} == "tbsp" && $x->{"quantity"} < 1) {
			$x->{"quantity"} *= 3;
			$x->{"abbrev"} = "tsp";
			$x->{"measurement"} = "Teaspoon";
		} else if($x->{"abbrev"} == "oz" && $x->{"quantity"} >= 4) {
			$x->{"quantity"} = (round($x->{"quantity"} * 250) / 1000);
			$x->{"abbrev"} = "";
			$x->{"measurement"} = "Pound";
		} else if($x->{"measurement"} == "Pound" && $x->{"quantity"} <= 0.25) {
			$x->{"quantity"} *= 4;
			$x->{"abbrev"} = "oz";
			$x->{"measurement"} = "Ounce";
		} else if($x->{"measurement"} == "Quarter" && $x->{"quantity"} >= 2) {
			$x->{"quantity"} = (round($x->{"quantity"} * 250) / 1000);
			$x->{"measurement"} = "Half";
		} else if($x->{"measurement"} == "Half" && $x->{"quantity"} >= 2) {
			$x->{"quantity"} = (round($x->{"quantity"} * 250) / 1000);
			$x->{"measurement"} = "Whole";
		} else if($x->{"measurement"} == "Whole" && $x->{"quantity"} == 0.5) {
			$x->{"quantity"} = $x->{"quantity"} * 2;
			$x->{"measurement"} = "Half";
		} else if($x->{"measurement"} == "Half" && $x->{"quantity"} == 0.5) {
			$x->{"quantity"} = $x->{"quantity"} * 2;
			$x->{"measurement"} = "Quarter";
		}
	}
}

function get($page,$data=NULL){
	$api = "http://localhost/spork/api/";
	$url = $api.$page;	
	
	if($data):
		foreach ($data as $key=>$value) 
			$string = "$key=$value&";
		if($string):
			$string = rtrim($string,"&");
			$url .= "?$string";
		endif;
	endif;
	
	$cookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';
	session_write_close();
 
	$headers = array(
		'Accept: application/json',
		'Content-Type: application/json',
	);
	 
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt( $ch, CURLOPT_COOKIE, $cookie );
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
	
}
?>